//
//  DVDView.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/3/29.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "DVDView.h"

@interface DVDView () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation DVDView

- (void)setButton:(UIButton *)button WithImageName:(NSString *)name
{
    UIImage *image = GET_IAMGE_WITH_NAME(name);
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setFrame:CGRectMake(0, 0, 30, 30)];
    imageView.center = CGPointMake(button.bounds.size.width / 2, button.bounds.size.height / 2);
    [button addSubview:imageView];
    imageView = nil;
}

- (UIScrollView *)scrollView
{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        _scrollView.delegate = self;
        _scrollView.backgroundColor = BACKGROUND_COLOR;
        _scrollView.scrollEnabled = YES;
    }
    return _scrollView;
}

- (void)addHead
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.bounds.size.width, self.bounds.size.height / 5)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.height / 2, view.bounds.size.height / 2)];
    leftButton.center = CGPointMake(35, view.bounds.size.height / 2);
    [leftButton setImage:[UIImage imageNamed:@"Image/fundationView/turnon"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(pressDVDOn) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.height / 2, view.bounds.size.height / 2)];
    rightButton.center = CGPointMake(view.bounds.size.width - 35, view.bounds.size.height / 2);
    [rightButton setImage:[UIImage imageNamed:@"Image/fundationView/turnoff"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(pressDVDOff) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(view.bounds.size.height / 4 + 55, view.bounds.size.height / 4 - 5, 100, 20)];
    label1.text = @"开启影碟机";
    label1.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label1.textColor = [UIColor blackColor];
    
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(view.bounds.size.height / 4 + 55, view.bounds.size.height / 4 + 20, 150, 40)];
    label2.text = @"只有开启影碟机才可以控制";
    label2.font = [UIFont fontWithName:@"Arial-BoldMT" size:15];
    label2.textColor = [UIColor lightGrayColor];
    [label2 setLineBreakMode:NSLineBreakByWordWrapping];
    label2.numberOfLines = 0;
    
    [view addSubview:leftButton];
    [view addSubview:rightButton];
    [view addSubview:label1];
    [view addSubview:label2];
    [self.scrollView addSubview:view];
    leftButton = nil;
    rightButton = nil;
    label1 = nil;
    label2 = nil;
    view = nil;
}

- (void)addFudationButton
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.bounds.size.height / 5 + 25, 50, 20)];
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label.text = @"操作";
    label.textColor = TEXT_GRAY_COLOR;
    [self.scrollView addSubview:label];
    label = nil;
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height / 5 + 55, self.bounds.size.width, 75)];
    view1.backgroundColor = [UIColor whiteColor];
    UIButton *button;
    for (int i = 0; i < 3; i++) {
        button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
        button.tag = i;
        if(i == 0){
            button.center = CGPointMake(self.bounds.size.width / 5 * 1, view1.bounds.size.height / 2);
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_play_n") forState:UIControlStateNormal];
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_play_p") forState:UIControlStateHighlighted];
        }else if(i == 1){
            button.center = CGPointMake(self.bounds.size.width / 2, view1.bounds.size.height / 2);
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_pause_n") forState:UIControlStateNormal];
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_pause_p") forState:UIControlStateHighlighted];
        }else{
            button.center = CGPointMake(self.bounds.size.width / 5 * 4, view1.bounds.size.height / 2);
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_stop_n") forState:UIControlStateNormal];
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_stop_p") forState:UIControlStateHighlighted];
        }
        [button addTarget:self action:@selector(pressFudationButton:) forControlEvents:UIControlEventTouchUpInside];
        [view1 addSubview:button];
    }
    [self.scrollView addSubview:view1];
    view1 = nil;
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height / 5 + 132, self.bounds.size.width, 75)];
    view2.backgroundColor = [UIColor whiteColor];
    for (int i = 0; i < 4; i++) {
        button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
        button.tag = i + 3;
        if(i == 0){
            button.center = CGPointMake(self.bounds.size.width / 7 * 1, view2.bounds.size.height / 2);
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_playprev_n") forState:UIControlStateNormal];
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_playprev_p") forState:UIControlStateHighlighted];
        }else if(i == 1){
            button.center = CGPointMake(self.bounds.size.width / 8 * 3, view2.bounds.size.height / 2);
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_prev_n") forState:UIControlStateNormal];
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_prev_p") forState:UIControlStateHighlighted];
        }else if(i == 2){
            button.center = CGPointMake(self.bounds.size.width / 8 * 5, view2.bounds.size.height / 2);
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_next_n") forState:UIControlStateNormal];
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_next_p") forState:UIControlStateHighlighted];
        }else{
            button.center = CGPointMake(self.bounds.size.width / 7 * 6, view2.bounds.size.height / 2);
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_playnext_n") forState:UIControlStateNormal];
            [button setImage:GET_IAMGE_WITH_NAME(@"Image/fundationView/video/player_playnext_p") forState:UIControlStateHighlighted];
        }
        [button addTarget:self action:@selector(pressFudationButton:) forControlEvents:UIControlEventTouchUpInside];
        [view2 addSubview:button];
    }
    button = nil;
    [self.scrollView addSubview:view2];
    view2 = nil;
}

- (void)addNumberButton
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, self.bounds.size.height / 5 + 222, 50, 20)];
    label.font = [UIFont fontWithName:@"Arial-BoldMT" size:20];
    label.text = @"操作";
    label.textColor = TEXT_GRAY_COLOR;
    [self.scrollView addSubview:label];
    label = nil;
    
    UIButton *button;
    for (int i = 0; i < 12; i++) {
        button = [[UIButton alloc] initWithFrame:CGRectMake((i % 3) * ((self.bounds.size.width -  8) / 6.0 + 1) ,
                                                            self.bounds.size.height / 5.0 + 252 + (i / 3) *((self.bounds.size.width -  8) / 6.0 + 1),
                                                            (self.bounds.size.width -  8) / 6.0, (self.bounds.size.width -  8) / 6.0)];
        button.tag = i;
        [button setBackgroundColor:[UIColor whiteColor]];
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if(i <= 8){
            [button setTitle:[NSString stringWithFormat:@"%d",i + 1] forState:UIControlStateNormal];
        }else if( i == 10){
            [button setTitle:@"0" forState:UIControlStateNormal];
        }
        button.userInteractionEnabled = YES;
        [button addTarget:self action:@selector(pressLeftButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
    
    for (int i = 0; i < 12; i++) {
        button = [[UIButton alloc] initWithFrame:CGRectMake(self.bounds.size.width - (3 - (i % 3)) * ((self.bounds.size.width -  8) / 6.0 + 1) ,
                                                            self.bounds.size.height / 5.0 + 252 + (i / 3) *((self.bounds.size.width -  8) / 6.0 + 1),
                                                            (self.bounds.size.width -  8) / 6.0, (self.bounds.size.width -  8) / 6.0)];
        button.tag = i;
        [button setBackgroundColor:[UIColor whiteColor]];
        button.titleLabel.font = [UIFont systemFontOfSize:20];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if (i == 1){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_up"];
        }else if (i == 2){
            [button setTitle:@"出碟" forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }else if (i == 3){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_left"];
        }else if (i == 4){
            [button setTitle:@"确认" forState:UIControlStateNormal];
            [button setTitleColor:TEXT_GREEN_COLOR forState:UIControlStateNormal];
        }else if (i == 5){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_right"];
        }else if (i == 6){
            [button setTitle:@"声道" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }else if (i == 7){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_down"];
        }else if (i == 8){
            [button setTitle:@"字幕" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }else if (i == 9){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_return"];
        }else if (i == 10){
            [button setTitle:@"退出" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }else if (i == 11){
            [self setButton:button WithImageName:@"Image/fundationView/TV/player_control_menu"];
        }
        button.userInteractionEnabled = YES;
        [button addTarget:self action:@selector(pressRightButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
    button = nil;
    
    self.scrollView.contentSize = CGSizeMake(self.bounds.size.width, self.bounds.size.height / 5 + 252 + (self.bounds.size.width -  8) / 3 * 2 + 15);
}

#pragma mark - 按钮响应函数
- (void)pressDVDOn
{
    //开
    [self.delegate sendRespondByFuctionName:DVD_ON];
}

- (void)pressDVDOff
{
    //关
    [self.delegate sendRespondByFuctionName:DVD_OFF];
}

- (void)pressFudationButton:(UIButton *)button
{
    switch (button.tag) {
        case 0:
            //播放
            [self.delegate sendRespondByFuctionName:DVD_PLAY];
            break;
        case 1:
            //暂停
            [self.delegate sendRespondByFuctionName:DVD_PAUSE];
            break;
        case 2:
            //停止
            [self.delegate sendRespondByFuctionName:DVD_STOP];
            break;
        case 3:
            //上一曲
            [self.delegate sendRespondByFuctionName:DVD_PREVIOUS];
            break;
        case 4:
            //快退
            [self.delegate sendRespondByFuctionName:DVD_BACKUP];
            break;
        case 5:
            //快进
            [self.delegate sendRespondByFuctionName:DVD_FORWARD];
            break;
        case 6:
            //下一曲
            [self.delegate sendRespondByFuctionName:DVD_NEXT];
            break;
        default:
            break;
    }
}

- (void)pressLeftButton:(UIButton *)button
{
    if(button.tag == 10){
        //数字0
        [self.delegate sendRespondByFuctionName:@"0"];
    }else if(button.tag < 9){
        //数字1-9
        [self.delegate sendRespondByFuctionName:[NSString stringWithFormat:@"%ld",(long)button.tag + 1]];
    }
}

- (void)pressRightButton:(UIButton *)button
{
    switch (button.tag) {
        case 1:
            //上
            [self.delegate sendRespondByFuctionName:DVD_UP];
            break;
        case 2:
            //出碟
            [self.delegate sendRespondByFuctionName:DVD_OPEN];
            break;
        case 3:
            //左
            [self.delegate sendRespondByFuctionName:DVD_LEFT];
            break;
        case 4:
            //确认
            [self.delegate sendRespondByFuctionName:DVD_OK];
            break;
        case 5:
            //右
            [self.delegate sendRespondByFuctionName:DVD_RIGHT];
            break;
        case 6:
            //声道
            [self.delegate sendRespondByFuctionName:DVD_TRACK];
            break;
        case 7:
            //下
            [self.delegate sendRespondByFuctionName:DVD_DOWN];
            break;
        case 8:
            //字幕
            [self.delegate sendRespondByFuctionName:DVD_CAPTION];
            break;
        case 9:
            //返回
            [self.delegate sendRespondByFuctionName:DVD_BACK];
            break;
        case 10:
            //退出
            [self.delegate sendRespondByFuctionName:DVD_EXIT];
            break;
        case 11:
            //菜单
            [self.delegate sendRespondByFuctionName:DVD_MENU];
            break;
        default:
            break;
    }
}

- (instancetype) initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame]){
        [self addSubview:self.scrollView];
        [self addHead];
        [self addFudationButton];
        [self addNumberButton];
    }
    
    return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
